package com.pzr.soccer.match.util;


import com.pzr.soccer.match.domain.Game;
import com.pzr.soccer.match.model.Expression;
import com.pzr.soccer.match.model.GameWrapper;
import com.pzr.soccer.plan.domain.Plan;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author pzr
 * @date:2021-12-09-17:36
 * @Description: 测试game wrapper 里面的game是否符合plan
 **/
@Slf4j
@Component
public class FindRightGameForExpressions {

    public static boolean testCondition(GameWrapper wrapper, Plan plan) {

        //currentTime 不在测试范围
        if (plan.getCondition_begin() > wrapper.getCurrentMin_revised() || plan.getCondition_end() < wrapper.getCurrentMin_revised()) {
            // log.info(wrapper.getCurrentMin_revised()+" not in period: expect: ["+plan.getCondition_begin()+","+plan.getCondition_end()+"]");
            return false;
        }
        int begin = plan.getCondition_begin();
        int end = wrapper.getCurrentMin_revised();
        return testExpressions(wrapper.getGame(), begin, end, plan.getPre_conditions());
    }


    public static boolean testResult(GameWrapper wrapper, Plan plan) {

        //currentTime 不在测试范围
        if (plan.getResult_begin() > wrapper.getCurrentMin_revised() || plan.getResult_end() < wrapper.getCurrentMin_revised()) {
            //  log.info(wrapper.getCurrentMin_revised()+" not in period: expect: ["+plan.getResult_begin()+","+plan.getResult_end()+"]");
            return false;
        }
        int begin = plan.getCondition_begin();
        int end = wrapper.getCurrentMin_revised();
        return testExpressions(wrapper.getGame(), begin, end, plan.getResult_conditions());
    }

    public static boolean testExpressions(Game game, int begin, int end, List<Expression> expressions) {
        for (Expression expression : expressions) {
            Object real_result = MatchCalculator.calculate(expression.getLeft(), game, new int[]{begin, end});
            if (!Comparer.compare(expression.getOperator(), real_result, expression.getRight())) {
                return false;
            }
            ;
        }
        return true;
    }

}
